/*
 * v4a_hal.h
 *
 *  Created on: Aug 6, 2013
 *      Author: rayin
 */




#ifndef V4A_HAL_H_
#define V4A_HAL_H_

#include <stdio.h>
#include "bmp_encoder.h"
#include "USER_CFG.h"

#define 	CSR_WRITE 	0x83
#define  	CSR_READ 	0x84
#define  	DAT_WRITE 	0x91
#define  	DAT_READ 	0x92
#define  	VCM_CBUSY 	0xa5

#define 	BKLAYER		0
#define 	VDLAYER 	1
#define 	UDLAYER		2
#define 	TDLAYER		3

#define		BKBITS		16
#define		VDBITS		16
#define		UDBITS		32
#define		TXBITS		32

extern unsigned int LCD_CLK;


typedef struct image_node{
	int 	index;
	int		img_base;
	int 	img_xlen;
	int 	img_ylen;
	int		img_bpp;	//16bit or 32bit
}IMGNODE;

IMGNODE imglist[16][128];	//swap all picture to v4a memory

typedef struct	layer_param{
	int		layer;
	int		bpp;
	int		vrambase;
	int		virtual_xlen;
	int		virtual_ylen;
	int		virtual_xoffset;
	int 		virtual_yoffset;
	int		screen_xlen;
	int		screen_ylen;
	int		screen_xoffset;
	int		screen_yoffset;
	int		auto_scroll;
	int		scroll_direction;
	int		scroll_speed;
	int		scroll_step;
	int		enable;
}LAYER;

LAYER	background;
LAYER	video;
LAYER	overlay;
LAYER	flex;
LAYER	menu;


typedef struct	gui_point{
	int		x;
	int 	y;
}GUIPOINT;

typedef struct gui_rect{
	struct	gui_point lvertex;
	unsigned int width;
	unsigned int height;
}GUIRECT;

union gui_color{
	struct color_component{
		unsigned char blue;
		unsigned char green;
		unsigned char red;
		unsigned char alpha;
		}component;
	unsigned int value;
}GUICOLOR;

#define COLOR unsigned int

#define ARGB(alpha,red,green,blue) (alpha<<24) + (red<<16) + (green<<8) + blue
#define RGB(red,green,blue) (0xff<<24) + (red<<16) + (green<8) + blue
#define TCOLOR(color) ((color & 0x00f80000) >> 8) + ((color & 0x0000fc00) >> 5) + ((color & 0x000000f8) >> 3)

#define BLUE 	0xff0000ff
#define GREEN 	0xff00ff00
#define RED 	0xffff0000
#define BLACK	0xff000000
#define WHITE	0xffffffff
#define YELLOW	0xffffff00


#define	 ERR_OOM	0x80		//out of memory
#define	 ERR_BWM	0x81		//bit width mismatch
#define ERR_FMT		0x82		//file format error

///////////////////v4a internal register list
#define V4A_MAXRESOLUTION 1366*768
#define V4A_MAXFLEXWINDOW 640*2048

typedef struct _ploader_reg_list{
	unsigned int source_base_address;
	unsigned int source_width;
	unsigned int source_height;
	unsigned int source_xstart;
	unsigned int source_ystart;
	unsigned int load_ylength;
	unsigned int load_xlength;
	unsigned int load_filldata;
	unsigned int sink_base_address;
	unsigned int sink_width;
	unsigned int sink_height;
	unsigned int sink_xstart;
	unsigned int sink_ystart;
	union _format{
		struct _ploader_format_reg{
			unsigned int source_order:1;
			unsigned int sink_is_32bit:1;
			unsigned int source_is_32bit:1;
			unsigned int fill_mode:1;
			unsigned int unused2:28;
		} format_reg;
		unsigned format_value;
	}format;
	union _ploader_ctrl{
		struct _ctrl_reg{
			unsigned int clear:1;
			unsigned int start:1;
			unsigned int unused3:30;
			}ctrl_reg;
		unsigned int ctrl_value;
	}ctrl;

}HDPLOADER;


#define		V4ABUSY			0x01

#define		HDPLOADERBASE 	0x20


typedef struct _slm_reg_list{
	unsigned int v0_baseaddr;
	unsigned short v1_xlength;
	unsigned short v1_ylength;
	unsigned short v1_xstart;
	unsigned short v1_ystart;
	unsigned int v2_baseaddr;
	unsigned short screen_xlength;
	unsigned short screen_ylength;
	unsigned int v3_baseaddr;
	unsigned short v3_vxlength;
	unsigned short v3_vylength;
	unsigned short v3_vxstart;
	unsigned short v3_vystart;
	unsigned short v3_oxlength;
	unsigned short v3_oylength;
	unsigned short v3_oxstart;
	unsigned short v3_oystart;
	unsigned int scrollparam;
	union _enable{
		struct _enable_all{
			unsigned int v0_enable:1;
			unsigned int v1_enable:1;
			unsigned int v2_enable:1;
			unsigned int v3_enable:1;
			unsigned int unused:28;
		}enablelist;
		unsigned enable_value;
	}enable;
	unsigned int setting;
	unsigned int outenable;
	unsigned int unused;
}LAYERMIXER;



#define LAYERMIXERBASE 0x00

#define V4A_VIDEOBASE 		0x01b50000
#define V4A_BKGNDBASE0 		V4A_VIDEOBASE - V4A_MAXRESOLUTION * 2
#define V4A_BKGNDBASE1 		V4A_BKGNDBASE0 - V4A_MAXRESOLUTION * 2
#define V4A_OVERLAYBASE0 	V4A_BKGNDBASE1 - V4A_MAXRESOLUTION * 4
#define V4A_OVERLAYBASE1 	V4A_OVERLAYBASE0 - V4A_MAXRESOLUTION * 4
#define V4A_FLEXBASE 		V4A_OVERLAYBASE1 -V4A_MAXFLEXWINDOW * 4

///////////////////////end of register list

////////////////gui base api
GUIPOINT *gui_point(int x,int y);

GUIRECT *gui_rect(int x,int y,int xlen,int ylen);
IMGNODE *img_node(int base,int bpp,int index,int xlen,int ylen);

////////////////////////////////////////////////
int v4a_initial();
void v4a_setregs(int addr,int length,int *regs);
void v4a_getregs(int addr,int length,int *regs);
int v4a_getstatus();

void v4a_fillvram(int addr,int length,int *buffer);

int v4a_initbmp(FILE *fp,IMGNODE *node);	//swap bitmap file to v4a-dram
int v4a_initraw(BMPINFO *info,IMGNODE *node);//swap bitmap buffer to v4a-dram

int	v4a_drawpoint(IMGNODE *node,GUIPOINT *p,COLOR c);
int	v4a_drawline();
int	v4a_drawrect(IMGNODE *node,GUIRECT *rect,COLOR c);

int	v4a_showimage(IMGNODE *node,LAYER *lp,GUIPOINT *p);
int	v4a_showimagecut(IMGNODE *node,LAYER *lp,GUIRECT *src_rect,GUIPOINT *sink_p);

int	v4a_copyimage(IMGNODE *src,IMGNODE *sink,GUIPOINT *p);
int	v4a_copyimagecut(IMGNODE *src,IMGNODE *sink,GUIRECT *src_rect,GUIPOINT *sink_p);

void v4a_initscreen(int xlength,int ylength);
void v4a_enablescreen(int enable);
void v4a_displaybkgnd(int enable);
void v4a_switchbkgnd();
void v4a_setvideoarea(GUIRECT *rect);
void v4a_displayvideo(int enable);
void v4a_displayoverlay(int enable);
void v4a_switchoverlay();
void v4a_displayflex(int enable);
void v4a_setautoscroll(int enable,int dir,int speed,int step);
void v4a_setflexarea(GUIRECT *vrect,GUIRECT *orect);


#endif /* V4A_HAL_H_ */
